package com.example.admin.simple_ding.main.model;

import com.example.admin.simple_ding.Utils.OkHttpUtils;
import com.example.admin.simple_ding.commons.Urls;
import com.example.admin.simple_ding.main.bean.NewsBean;
import com.example.admin.simple_ding.main.widget.MainActivity;
import com.example.admin.simple_ding.main.widget.NewsJsonUtils;

import java.util.List;

/**
 * Created by admin on 2016/4/27.
 */
public class NewsModelImpl implements NewsModel {

    @Override
    public void loadNews(String url, final int type, final OnLoadNewsListListener listener) {
        OkHttpUtils.ResultCallback<String> loadNewsCallback = new OkHttpUtils.ResultCallback<String>() {
            @Override
            public void onSuccess(String response) {
                List<NewsBean> newsBeanList = NewsJsonUtils.readJsonNewsBeans(response, getID(type));
                listener.onSuccess(newsBeanList);
            }
            @Override
            public void onFailure(Exception e) {
                listener.onFailure("load news list failure.", e);
            }
        };
        OkHttpUtils.get(url, loadNewsCallback);
    }

    /**
     * 获取ID
     * @param type
     * @return
     */
    private String getID(int type) {
        String id;
        switch (type) {
            case MainActivity.NEWS_TYPE_TOP:
                id = Urls.TOP_ID;
                break;
            case MainActivity.NEWS_TYPE_NBA:
                id = Urls.NBA_ID;
                break;
            case MainActivity.NEWS_TYPE_CARS:
                id = Urls.CAR_ID;
                break;
            case MainActivity.NEWS_TYPE_JOKES:
                id = Urls.JOKE_ID;
                break;
            default:
                id = Urls.TOP_ID;
                break;
        }
        return id;
    }
    public interface OnLoadNewsListListener {
        void onSuccess(List<NewsBean> list);
        void onFailure(String msg, Exception e);
    }

   /* public interface OnLoadNewsDetailListener {
        void onSuccess(NewsDetailBean newsDetailBean);
        void onFailure(String msg, Exception e);
    }
*/
}
